﻿using BCEntity.OA.PageBusiness.PageBusinessMappingDepartment;
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;
namespace BCData.OA.PageBusiness.PageBusinessMappingDepartment
{
    /// <summary>
    /// OA系统页面业务映射部门
    /// </summary>
	public class PageBusinessMappingDepartmentData : IPageBusinessMappingDepartmentData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY_EXISTS = @"SELECT 1 FROM OA_PageBusinessMappingDepartment";
        private const string QUERY = @"SELECT PageBusinessMappingDepartmentId,AllowJobTitle,AllowJobTitleStr,CreateTime,PageBusinessConfigId,PageBusinessCode,PageBusinessName,BelongCompanyId,
                                            BelongCompanyName,DepartmentId,DepartmentName,CompanyId,CompanyName,IsAllJobTitle,BusinessFlowModelId,BusinessFlowModelName,IsAutoAssign,
                                            AssignRuleType,IsEnabledAuxiliaryRule,PredefinedTimeoutHour,NotificationCategoryId,NotificationCategoryCode,NotificationCategoryName,IsDynamicCompany
                                        FROM OA_PageBusinessMappingDepartment";
        public PageBusinessMappingDepartmentData(IDatabaseContext databaseContext)
        {
            this.databaseContext=databaseContext;
        }

        public async Task<bool> Exists(long pageBusinessMappingDepartmentId)
        {
            string sql = string.Format(@"{0} WHERE PageBusinessMappingDepartmentId=?PageBusinessMappingDepartmentId", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?PageBusinessMappingDepartmentId", pageBusinessMappingDepartmentId, DbType.Int64);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }

        public async Task<bool> Exists(string pageBusinessMappingDepartmentName)
        {
            string sql = string.Format(@"{0} WHERE PageBusinessMappingDepartmentName=?PageBusinessMappingDepartmentName", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?PageBusinessMappingDepartmentName", pageBusinessMappingDepartmentName, DbType.AnsiString);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }

        public async Task<PageBusinessMappingDepartmentEntity> Add(PageBusinessMappingDepartmentEntity entity)
        {
            string sql = SqlCommands.GetInsertSql("OA_PageBusinessMappingDepartment", new string[] { "PageBusinessConfigId", "PageBusinessCode", "PageBusinessName","BelongCompanyId",
                "BelongCompanyName", "DepartmentId","DepartmentName", "CompanyId", "CompanyName", "IsAllJobTitle", "AllowJobTitle","AllowJobTitleStr","BusinessFlowModelId","BusinessFlowModelName",
                "IsAutoAssign","AssignRuleType","IsEnabledAuxiliaryRule","PredefinedTimeoutHour","NotificationCategoryId","NotificationCategoryCode","NotificationCategoryName","IsDynamicCompany"});
            sql = string.Format("{0} {1} WHERE PageBusinessMappingDepartmentId=LAST_INSERT_ID();", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?PageBusinessConfigId", entity.PageBusinessConfigId, DbType.Int64);
            parameters.Add("?PageBusinessCode", entity.PageBusinessCode, DbType.AnsiString);
            parameters.Add("?PageBusinessName", entity.PageBusinessName, DbType.AnsiString);
            parameters.Add("?DepartmentId", entity.DepartmentId, DbType.Int32);
            parameters.Add("?DepartmentName", entity.DepartmentName, DbType.AnsiString);
            parameters.Add("?BelongCompanyId", entity.BelongCompanyId, DbType.Int32);
            parameters.Add("?BelongCompanyName", entity.BelongCompanyName, DbType.AnsiString);
            parameters.Add("?CompanyId", entity.CompanyId, DbType.Int32);
            parameters.Add("?CompanyName", entity.CompanyName, DbType.AnsiString);
            parameters.Add("?IsAllJobTitle", entity.IsAllJobTitle, DbType.Boolean);
            parameters.Add("?AllowJobTitle", entity.AllowJobTitle, DbType.AnsiString,size:1000);
            parameters.Add("?AllowJobTitleStr", entity.AllowJobTitleStr, DbType.AnsiString, size: 1000);
            parameters.Add("?BusinessFlowModelId", entity.BusinessFlowModelId, DbType.Int64);
            parameters.Add("?BusinessFlowModelName", entity.BusinessFlowModelName, DbType.AnsiString,size:20);
            parameters.Add("?IsAutoAssign", entity.IsAutoAssign, DbType.Boolean);
            parameters.Add("?AssignRuleType", entity.AssignRuleType?.ToString(), DbType.AnsiString);
            parameters.Add("?IsEnabledAuxiliaryRule", entity.IsEnabledAuxiliaryRule, DbType.Boolean);
            parameters.Add("?PredefinedTimeoutHour", entity.PredefinedTimeoutHour, DbType.Int32);
            parameters.Add("?NotificationCategoryId", entity.NotificationCategoryId, DbType.Int64);
            parameters.Add("?NotificationCategoryCode", entity.NotificationCategoryCode, DbType.AnsiString, size: 50);
            parameters.Add("?NotificationCategoryName", entity.NotificationCategoryName, DbType.AnsiString, size: 30);
            parameters.Add("?IsDynamicCompany", entity.IsDynamicCompany, DbType.Boolean);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<PageBusinessMappingDepartmentEntity>(sql, parameters, databaseContext.Transaction);

        }

        public async Task<PageBusinessMappingDepartmentEntity> Update(PageBusinessMappingDepartmentEntity entity)
        {
            string sql = SqlCommands.GetUpdateSql("OA_PageBusinessMappingDepartment", new string[] { "PageBusinessConfigId", "PageBusinessCode", "PageBusinessName", "DepartmentId",
                "DepartmentName", "CompanyId", "CompanyName", "IsAllJobTitle", "AllowJobTitle","AllowJobTitleStr","BusinessFlowModelId","BusinessFlowModelName","IsAutoAssign",
                "AssignRuleType","IsEnabledAuxiliaryRule","PredefinedTimeoutHour","NotificationCategoryId","NotificationCategoryCode","NotificationCategoryName","IsDynamicCompany"}, new string[] { "PageBusinessMappingDepartmentId" });
            sql = string.Format("{0} {1} WHERE PageBusinessMappingDepartmentId=?PageBusinessMappingDepartmentId;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?PageBusinessMappingDepartmentId", entity.PageBusinessMappingDepartmentId, DbType.Int64);
            parameters.Add("?PageBusinessConfigId", entity.PageBusinessConfigId, DbType.Int64);
            parameters.Add("?PageBusinessCode", entity.PageBusinessCode, DbType.AnsiString);
            parameters.Add("?PageBusinessName", entity.PageBusinessName, DbType.AnsiString);
            parameters.Add("?DepartmentId", entity.DepartmentId, DbType.Int32);
            parameters.Add("?DepartmentName", entity.DepartmentName, DbType.AnsiString);
            parameters.Add("?CompanyId", entity.CompanyId, DbType.Int32);
            parameters.Add("?CompanyName", entity.CompanyName, DbType.AnsiString);
            parameters.Add("?IsAllJobTitle", entity.IsAllJobTitle, DbType.Boolean);
            parameters.Add("?AllowJobTitle", entity.AllowJobTitle, DbType.AnsiString, size: 1000);
            parameters.Add("?AllowJobTitleStr", entity.AllowJobTitleStr, DbType.AnsiString, size: 1000);
            parameters.Add("?BusinessFlowModelId", entity.BusinessFlowModelId, DbType.Int64);
            parameters.Add("?BusinessFlowModelName", entity.BusinessFlowModelName, DbType.AnsiString, size: 20);
            parameters.Add("?IsAutoAssign", entity.IsAutoAssign, DbType.Boolean);
            parameters.Add("?AssignRuleType", entity.AssignRuleType?.ToString(), DbType.AnsiString);
            parameters.Add("?IsEnabledAuxiliaryRule", entity.IsEnabledAuxiliaryRule, DbType.Boolean);
            parameters.Add("?PredefinedTimeoutHour", entity.PredefinedTimeoutHour, DbType.Int32);
            parameters.Add("?NotificationCategoryId", entity.NotificationCategoryId, DbType.Int64);
            parameters.Add("?NotificationCategoryCode", entity.NotificationCategoryCode, DbType.AnsiString, size: 50);
            parameters.Add("?NotificationCategoryName", entity.NotificationCategoryName, DbType.AnsiString, size: 30);
            parameters.Add("?IsDynamicCompany", entity.IsDynamicCompany, DbType.Boolean);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<PageBusinessMappingDepartmentEntity>(sql, parameters, databaseContext.Transaction);
        }

        public async Task<bool> Delete(long pageBusinessMappingDepartmentId)
        {
            string sql = @"DELETE FROM OA_PageBusinessMappingDepartment WHERE PageBusinessMappingDepartmentId=?PageBusinessMappingDepartmentId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?PageBusinessMappingDepartmentId", pageBusinessMappingDepartmentId, DbType.Int64);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }

        public async Task<PageBusinessMappingDepartmentEntity> GetEntity(long pageBusinessMappingDepartmentId)
        {
            string sql = string.Format(@"{0} WHERE PageBusinessMappingDepartmentId=?PageBusinessMappingDepartmentId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?PageBusinessMappingDepartmentId", pageBusinessMappingDepartmentId, DbType.Int64);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<PageBusinessMappingDepartmentEntity>(sql, parameters);
        }

        public async Task<IEnumerable<PageBusinessMappingDepartmentEntity>> GetEntitiesByPageBusinessConfigId(long pageBusinessConfigId)
        {
            string sql = string.Format(@"{0} WHERE PageBusinessConfigId=?PageBusinessConfigId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?PageBusinessConfigId", pageBusinessConfigId, DbType.Int64);
            return await this.databaseContext.Slave.QueryAsync<PageBusinessMappingDepartmentEntity>(sql, parameters);
        }

        public async Task<IEnumerable<PageBusinessMappingDepartmentEntity>> GetList(int companyId)
        {
            string sql = string.Format("{0} WHERE BelongCompanyId=?BelongCompanyId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BelongCompanyId", companyId, DbType.Int32);
            return await this.databaseContext.Slave.QueryAsync<PageBusinessMappingDepartmentEntity>(sql, parameters);
        }

        public async Task<IEnumerable<PageBusinessMappingDepartmentEntity>> GetList(int companyId, long pageBusinessConfigId)
        {
            string sql = string.Format("{0} WHERE BelongCompanyId=?BelongCompanyId AND PageBusinessConfigId=?PageBusinessConfigId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BelongCompanyId", companyId, DbType.Int32);
            parameters.Add("?PageBusinessConfigId", pageBusinessConfigId, DbType.Int64);
            return await this.databaseContext.Slave.QueryAsync<PageBusinessMappingDepartmentEntity>(sql, parameters);
        }

        public async Task<(IEnumerable<PageBusinessMappingDepartmentEntity>, int)> Query(int companyId, long pageBusinessConfigId,string pageBusinessName, DateTime? start, DateTime? end, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            wheres.Add("BelongCompanyId=?BelongCompanyId");
            parameters.Add("?BelongCompanyId", companyId, DbType.Int32);
            wheres.Add("PageBusinessConfigId=?PageBusinessConfigId");
            parameters.Add("?PageBusinessConfigId", pageBusinessConfigId, DbType.Int64);
            if (!string.IsNullOrWhiteSpace(pageBusinessName))
            {
                wheres.Add("PageBusinessName LIKE CONCAT('%',CONCAT(?PageBusinessName,'%'))");
                parameters.Add("?PageBusinessName", pageBusinessName, DbType.AnsiString);
            }
            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("CreateTime>=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            string sql = string.Format(@"{0} {1} ORDER BY CreateTime DESC LIMIT {2},{3}; SELECT COUNT(1) FROM OA_PageBusinessMappingDepartment {1}",
                   QUERY,
                   wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                   (pageIndex - 1) * pageSize,
                   pageSize);
            return await this.databaseContext.Slave.QueryPageableListAsync<PageBusinessMappingDepartmentEntity>(sql, parameters);
        }
    }
}
